
<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_ADMIN_MNG;
$page_pageId = '个人信息';

require "../framepages/moudle_base.php";


$id = $_SESSION['USER'];

$res_admin_user = \Data\Tree_admin_user_DAO::getRecord($id);

$admin_name = $res_admin_user->getNAME();

$mode = $_POST['mode'];
$result = "normal";
if($mode == 'update_name'){
    $admin_name = $_POST['admin_name'];
    $res_admin_user->setNAME("$admin_name");
    \Data\Tree_admin_user_DAO::update($res_admin_user);

    $result = "succ_update";

}

if($mode == 'update_password'){
    $old_password = $_POST['old_password'];
    // 数据库原密码
    $password_info = $res_admin_user->getPASSWORD();
    if (md5($old_password) != $password_info) {
        $result = "err_old_password";
    } else {
        $new_password1 = $_POST['new_password1'];
        $new_password2 = $_POST['new_password2'];
        if (!$new_password1) {
            $result = "err_empty_new_password1";
        } elseif (!$new_password2) {
            $result = "err_empty_new_password2";
        } elseif ($new_password1 != $new_password2) {
            $result = "err_unsame_new_password";
        } else {
            $res_admin_user->setPASSWORD(md5($new_password1));
            \Data\Tree_admin_user_DAO::update($res_admin_user);
            $result = "succ_update";
        }
    }
}

?>
<div class="main-content">
    <div class="row">
        <div class="col-md-4">
            <div>
                <h3>用户名修改：</h3>
            </div>
            <form id="tab1" method="post" action="" onSubmit="javascript:return window.confirm('确认提交吗？')">

                <input type="hidden" name="mode" id="mode" value='update_name'>

                <div class="form-group">
                    <label for="admin_name">用户名：</label>
                    <input type="text" class="form-control span12" name="admin_name" id="admin_name" value="<?php echo $admin_name ?>">
                </div>

                <div class="btn-toolbar list-toolbar">
                    <input type="submit" class="btn btn-danger" value="修改">
                </div>

            </form>
        </div>
        <div class="col-md-4" style="margin-left:10%">
            <div>
                <h3>密码修改：</h3>
            </div>
            <form id="tab2" method="post" action="" onSubmit="javascript:return window.confirm('确认提交吗？')">

                <input type="hidden" name="mode" id="mode" value='update_password'>

                <div class="form-group">
                    <label for="old_password">原密码：</label>
                    <input type="password" class="form-control span12" name="old_password" id="old_password">
                </div>

                <div class="form-group">
                    <label for="new_password1">新密码：</label>
                    <input type="password" class="form-control span12" name="new_password1" id="new_password1">
                </div>
                <div class="form-group">
                    <label for="new_password2">再次输入新密码：</label>
                    <input type="password" class="form-control span12" name="new_password2" id="new_password2">
                </div>

                <div class="btn-toolbar list-toolbar">
                    <input type="submit" class="btn btn-danger" value="修改">
                </div>
            </form>
        </div>
    </div>

    <?php
    require "../framepages/footer.php";
    ?>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>

<script type="text/javascript">
    function init(){
        var msg = "<?php echo $result;?>";
        if (msg == "succ_update") {
            alert("修改成功！");
        }
        if (msg == 'err_old_password') {
            alert('修改失败，旧密码不正确');
        }
        if (msg == 'err_empty_new_password1') {
            alert('修改失败，新密码为空');
        }
        if (msg == 'err_empty_new_password2') {
            alert('修改失败，再次输入新密码为空');
        }
        if (msg == 'err_unsame_new_password') {
            alert('修改失败，两次新密码不一致');
        }
    }
</script>
</body>
</html>
